package jy.game.components.resource;

import java.util.ArrayList;
import java.util.List;

import jy.game.BasePojo;
import jy.game.components.role.JyRole;
import jy.game.tools.JyResourceManager;
import wx.xml.simpleframework.xml.Attribute;
import wx.xml.simpleframework.xml.Root;


@Root(name = "resource")
public class JyResource extends BasePojo {
    private static List<String> suggestTips = new ArrayList<>();
    @Attribute(name = "key") public   String key;
    @Attribute(name = "value") public String value;
    @Attribute(name = "icon") public  String icon;

//        public static Sprite GetZhujueHead() {
//            return JyResource.GetHeadImage(RuntimeData.Instance.team[0].head, false);
//        }

//    private static boolean suggestTipInitialized = false;


//        public static Sprite GetIcon(String iconName) {
//            if (CommonSettings.MOD_KEY >= 0) {
//                Sprite sprite = ModEditorResourceManager.GetSprite("Icons/" + iconName);
//                if (sprite != null) {
//                    return sprite;
//                }
//            }
//            return Resources.Load<Sprite>("Icons/" + iconName);
//        }


//        public static Sprite GetBattleBg(String key) {
//            if (CommonSettings.MOD_KEY >= 0) {
//                Sprite sprite = ModEditorResourceManager.GetSprite("BattleBg/" + key);
//                if (sprite != null) {
//                    return sprite;
//                }
//            }
//            return AssetBundleManager.GetBattleBg(key);
//        }
//
//
//        public static Sprite GetImage(String key, boolean  forceLoadFromResource = false) {
//            JyResource resource = JyResourceManager.get("JyResource",key);
//            if (resource == null) {
//                return null;
//            }
//            String text = resource.value.split(new char[]
//            {
//                '.'
//            })[0];
//            if (CommonSettings.MOD_KEY >= 0 && !forceLoadFromResource) {
//                Sprite sprite = ModEditorResourceManager.GetSprite(text);
//                if (sprite != null) {
//                    return sprite;
//                }
//            }
//            if (key.StartsWith("地图.", )) {
//                return AssetBundleManager.GetMap(text.split(new char[]
//                {
//                    '/'
//                }).Last<String>());
//            }
//            return Resources.Load<Sprite>(text);
//        }
//
//
//        private IEnumerator LoadTextureFromURL(String url, Action<Texture2D> cb) {
//            WWW www = new WWW(url);
//            yield return www;
//            if (TextUtils.isEmpty(www.error)) {
//                cb(www.texture);
//                www.Dispose();
//            }
//            yield break;
//        }


    //        public static void GetMusic(String key, Action<AudioClip> callback) {
//            JyResource resource = JyResourceManager.get("JyResource",key);
//            if (resource == null) {
//                // FileLogger.instance.Log("load invalid music, key=" + key);
//                callback(null);
//                return;
//            }
//            String text = resource.value.split(new char[]
//            {
//                '.'
//            })[0];
//            String url = text.split(new char[]
//            {
//                '/'
//            }).Last<String>();
//            if (CommonSettings.MOD_KEY >= 0 && ModEditorResourceManager.audios.containsKey(text)) {
//                ModEditorResourceManager.GetAudioClip(text, delegate (AudioClip acc) {
//                    callback(acc);
//                });
//            } else {
//                AudioClip audio = AssetBundleManager.GetAudio(url);
//                callback(audio);
//            }
//        }


//        public static byte[] GetBytes(String path, boolean  loadFromAssetBundle = true) {
//            if (loadFromAssetBundle) {
//                return null;
//            }
//            return Resources.Load<TextAsset>(path).bytes;
//        }

//    public static String GetRandomSuggestTip() {
//        if (!JyResource.suggestTipInited) {
//            for (Object object : JyResourceManager.getAll(JyResource.class)) {
//                JyResource resource = (JyResource)object;
//                if (resource.Key.startsWith("小贴士")) {
//                    JyResource.suggestTips.add(resource.value);
//                }
//            }
//            JyResource.suggestTipInited = true;
//        }
//        if (JyResource.suggestTips.size() == 0) {
//            JyResource.suggestTipInited = false;
//            return "";
//        }
//        return JyResource.suggestTips.get(Tools.getRandomInt(0, JyResource.suggestTips.size() - 1));
//    }


//        public static Sprite GetHeadImage(String key, boolean  forceLoadFromResource) {
//            JyResource resource = JyResourceManager.get("JyResource",key);
//            if (resource == null) {
//                return null;
//            }
//            String text = resource.value.split(new char[]
//            {
//                '.'
//            })[0];
//            if (CommonSettings.MOD_KEY >= 0 && !forceLoadFromResource) {
//                Sprite sprite = ModEditorResourceManager.LoadHeadSprite(ModEditorResourceManager.sprites[text]);
//                if (sprite != null) {
//                    return sprite;
//                }
//            }
//            return Resources.Load<Sprite>(text);
//        }

    public static String getLiezhuan(JyRole role) {
        JyResource resource;
        if (role.key == "主角") {
            resource = JyResourceManager.get(JyResource.class, "人物.主角");
        } else if (role.key == "女主") {
            resource = JyResourceManager.get(JyResource.class, "人物.女主");
        } else {
            resource = JyResourceManager.get(JyResource.class, "人物." + role.name);
            if (resource == null) {
                resource = JyResourceManager.get(JyResource.class, "人物." + role.key);
            }
        }
        if (resource == null) {
            return null;
        }
        return resource.value;
    }

    public static String getTalentDesc(String talent) {
        JyResource resource = JyResourceManager.get(JyResource.class, "天赋." + talent);
        if (resource == null) {
            return null;
        }
        return resource.value.split("#")[1];
    }

    public static int getTalentCost(String talent) {
        JyResource resource = JyResourceManager.get(JyResource.class, "天赋." + talent);
        if (resource == null) {
            return 0;
        }
        return Integer.parseInt(resource.value.split("#")[0]);
    }

    public static String getTalentInfo(String name, boolean displayCost) {
        String talentDesc = JyResource.getTalentDesc(name);
        if (displayCost) {
            return String.format("【%s】(消耗武学常识:%s)%s", name, JyResource.getTalentCost(name), talentDesc);
        }
        return String.format("【%s】%s", name, talentDesc);
    }

    // (get) Token: 0x0600043F RID: 1087 RVA: 0x000045BE File Offset: 0x000027BE
    @Override public /*override*/ String getPK() {
        {
            return this.key;
        }
    }
}
